package top.kaoshanji.p2ch08a;

import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.ClassVisitor;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.internal.org.objectweb.asm.Opcodes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;

/**
 * 代码清单-p2ch08a-260页a：统计方法个数的ClassVisitor接口的实现
 * author kaoshanji
 * ^_^
 * create time 2025/1/22 18:51
 */
public class ASMExample01 extends ClassVisitor {

    private static final Logger logger = LogManager.getLogger(ASMExample01.class);

    public static void main(String[] args) throws IOException {
        logger.info("...代码清单-p2ch08a-260页a：统计方法个数的ClassVisitor接口的实现...");

        ClassReader reader = new ClassReader("java.lang.String");
        ASMExample01 example = new ASMExample01(Opcodes.ASM5);
        reader.accept(example, 0);
        int count = example.getCount();
        logger.info("...count:{}...", count);
    }

    private int count = 0;

    public ASMExample01(int api) {
        super(api);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        count++;
        return null;
    }

    public int getCount() {
        return count;
    }
}
